<template>
  <ele-page>
    <!-- 搜索表单 -->
    <info-search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        :highlight-current-row="true"
        :export-config="{ fileName: '就业统计-学生基础信息' }"
        cache-key="statisticInfoTable"
        :loadOnCreated="false"
        :loading="loading"
      >
        <template #toolbar>
          <el-button
            class="ele-btn-icon"
            :icon="DownloadOutlined"
            @click="exportData"
          >
            导出
          </el-button>
        </template>
        <template #action="{ row }">
          <el-link type="primary" @click="openDetail(row)"> 查看</el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <info-detail v-model="showEdit" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { ElMessageBox } from 'element-plus';
  import { EleMessage } from 'ele-admin-plus';
  import InfoSearch from './components/student-info-search.vue';
  import InfoDetail from './components/student-info-detail.vue';
  import {
    pageInfos,
    exportInfos
  } from '@/api/manage/statistic/counsellor/studentInfo';
  import { DownloadOutlined } from '@/components/icons/index.js';

  defineOptions({ name: 'StatisticInfo' });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'code',
      label: '学号',
      align: 'center',
      minWidth: 90
    },
    {
      prop: 'name',
      label: '姓名',
      align: 'center',
      minWidth: 90
    },
    {
      prop: 'campus',
      label: '校区',
      align: 'center',
      minWidth: 110
    },
    {
      prop: 'grade',
      label: '年级',
      align: 'center',
      minWidth: 90
    },
    {
      prop: 'department',
      label: '系别',
      align: 'center',
      minWidth: 110
    },
    {
      prop: 'major',
      label: '专业',
      align: 'center',
      minWidth: 110
    },
    {
      prop: 'graduationYear',
      label: '毕业年份',
      align: 'center',
      minWidth: 110
    },
    {
      prop: 'organization',
      label: '组织',
      align: 'center',
      minWidth: 110
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 160,
      align: 'center',
      slot: 'action',
      hideInPrint: true,
      hideInExport: true
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表格数据源 */
  const datasource = ({ pages, where }) => {
    return pageInfos({ ...where, ...pages });
  };

  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 导出数据 */
  const exportData = () => {
    const loading = EleMessage.loading({
      message: '请求中..',
      plain: true
    });
    tableRef.value?.fetch?.(({ where }) => {
      exportInfos(where)
        .then(() => {
          loading.close();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error({ message: e.message, plain: true });
        });
    });
  };

  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row ?? null;
    showDetail.value = true;
  };
</script>
